package com.zjq.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zjq.pojo.User;
import com.zjq.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    @Qualifier("UserServiceImpl")
    private UserService userService;

    /*查询全部用户*/
    @RequestMapping("/alluser")
    public String queryAllUser(@RequestParam(value = "pn", required = true, defaultValue = "1") Integer pn, HttpServletRequest request, Model model){
        PageHelper.startPage(pn, 5);
        List<User> list = userService.queryAllUser();
        System.out.println("=======================>>>>");
        PageInfo<User> page = new PageInfo<User>(list, 5);
        model.addAttribute("user",page);
        return "backstage/user/alluser";
    }

    /*用户名查询用户*/
    @RequestMapping("/queryUserByUserName")
    @ResponseBody
    public List<User> queryUserByUserName(String userName, Model model){
        List<User> user = userService.queryUserByUserName(userName);
        model.addAttribute("user",user);
        model.addAttribute("key",userName);
        return user;
    }

    /*用户类型查询*/
    @RequestMapping("/queryUserByType")
    public String queryUserByType(int type,@RequestParam(value = "pn" , required = true , defaultValue = "1")Integer pn,Model model){
        List list = userService.queryUserByType(type);
        PageHelper.startPage(type,5);
        PageInfo pageInfo = new PageInfo<User>(list, 5);
        model.addAttribute("user",pageInfo);
        return "alluser";
    }

    /*添加用户*/
    @RequestMapping("/toaddUser")
    public String toaddUser(){
        return "/backstage/user/adduser";
    }

    @RequestMapping("/addUser")
    @ResponseBody
    public String addUser(User user,@RequestParam("file") MultipartFile file) throws IOException {
        String userName = user.getUserName();
        List<User> users = userService.queryUserByUserName(userName);
        System.out.println("=========================>>>>>>>>>>>>>>>>>>>>>>>users"+users);
        if (users.size() == 0){
            System.out.println(file);
            String filename = "/images/"+file.getOriginalFilename();
            user.setAvatar(filename);
            user.setCreateTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
            userService.addUser(user);
            String url = "D:\\bookImg";
            File file1 = new File(url, file.getOriginalFilename());
            file.transferTo(file1);
            int pn =userService.queryAllUser().size() / 5;
            if (pn == 0){
                return "<script>window.location.href='alluser?pn="+pn+"'</script>";
            }else {
                pn++;
                return "<script>window.location.href='alluser?pn="+pn+"'</script>";
            }
        }else {
            return "用户已存在";
        }
    }

    /*删除用户*/
    @RequestMapping("/deleteUser")
    public String deleteUser(int id){
        userService.deleteUser(id);
        return "redirect:/user/alluser";
    }

    /*更新用户*/
    @RequestMapping("/toupdateUser")
    public String toupdateUser(int id,Model model){
        User user = userService.queryUserById(id);
        System.out.println("---------------------"+user);
        model.addAttribute("user",user);
        return "backstage/user/updateuser";
    }

    @RequestMapping("/updateuser")
    public String updateUser(User user){
        System.out.println("1111111111111111111111111111111111111111"+user);
        userService.updateUser(user);
        return "backstage/user/alluser";
    }

    /*用户登录*/
    @RequestMapping("/tologin")
    public String tologin(){
        return "backstage/user/login";
    }
    @RequestMapping("/login")
    @ResponseBody
    public Boolean login(String userName,String password,HttpSession session){
        List<User> users = userService.queryUserByUserName(userName);
        System.out.println("=====-=--=-=-=-=-=-=-=-=-login  "+userName);
        for (User user : users){
            if (user.getPassword().equals(password)){
                session.setAttribute("user",user);
                System.out.println("11111111111111111"+session.getAttribute("user"));
                return true;
            }
        }
        return false;
    }

    @RequestMapping("/removeSession")
    @ResponseBody
    public String removeSession(HttpServletRequest request){
        request.getSession().invalidate();
        return "<script>location.reload();</script>";
    }

}
